import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import {createStyleImportPlugin,AntdResolve} from 'vite-plugin-style-import'
import path from 'path'
import {existsSync} from 'fs'
// https://vitejs.dev/config/
// 获取antd样式路径，一是因为form-item这种无文件夹，二是theme这种有文件夹无style
function getStylePath(name) {
  const names = name.split("-");
  const pathCss = `antd/es/${name}/style/index.js`;
  if (existsSync(path.join(__dirname, "./node_modules/" + pathCss))) {
    return pathCss;
  } else {
    names.pop()
    return getStylePath(names.join("-")) || ""
  }
}

export default defineConfig({
  plugins: [
    react(),
    createStyleImportPlugin({
      resolves:[
        AntdResolve()
      ],
      libs: [
        // If you don’t have the resolve you need, you can write it directly in the lib, or you can provide us with PR
        {
          libraryName: 'antd',
          esModule: true,
          resolveStyle: (name) => {
            return getStylePath(name)
          },
        },
      ],
    })
  ],
  resolve:{
    alias:{
      "@":path.resolve(__dirname,'./src')
    }
  }
})
